package com.example.controller;


import com.example.pojo.Article;
import com.example.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/article")
@CrossOrigin
@Api(value = "article", description = "article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation(notes = "article_save", value = "article_save")
    @RequestMapping("article_save")
    public String save(Article article) {
        return articleService.save(article).toString();
    }

    @ApiOperation(notes = "by_key", value = "by_key")
    @RequestMapping("/{key}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public Page<Article> save(@PathVariable String key, Pageable pageable) {
        Page<Article> byKey = articleService.findByKey(key, pageable);
        return byKey;
    }
}
